package com.hivision.alimqttapi.util;

import com.hivision.iot.api.AliReqClient;
import com.hivision.iot.dto.AssetTechnologyAttrResponseDTO;
import com.hivision.iot.dto.Results;
import com.hivision.iot.dto.Series;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Component
@RestController
@RequestMapping("/att")
public class AttributeValue {

    @Autowired
    AliReqClient reqClient;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @GetMapping("/test")
    public String test(String accessCode, Map<String, Object> map) {
//        List<String> list = new ArrayList<>();
//        list.add("car_class");
//        map.put("AttributeCodes",list);
        map.put("Type", "last");
        AssetTechnologyAttrResponseDTO roll_bed_rfid_out_welding1 = reqClient.getAssetTechnologyAttrData(accessCode, map);
        List<Results> data = roll_bed_rfid_out_welding1.getResults();
        if (data != null) {
            // 从 data 中获取 Results 对象
            Results results = data.get(0);

            // 从 Results 对象中获取 Series 对象
            List<Series> series = results.getSeries();

            // 从 Series 对象中获取 values 列表
            Series seriesData = series.get(0);

            List<List<Object>> values = seriesData.getValues();
            // 获取 values 列表中第一个元素（即 [1718096668987, String 0, 16]）的第二个值

            List<Object> vinValues = values.get(0);

            String vin = (String) vinValues.get(1);
            int index = values.size();
//            System.out.println("/////////////////////////////////////////////////");
//            System.out.println(roll_bed_rfid_out_welding1);
//            System.out.println(data);
//            System.out.println("values：" + values);
//            System.out.println(values.get(index - 1).get(1));
            if (values.get(index - 1).size() > 5) {
                return String.valueOf(values.get(index - 2).get(1)).substring(0, 2);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @GetMapping("/readNum02")
    public String readNum02() {
        Map<String, Object> map=new HashMap<>();
        String accessCode="RB764_pass";
        List<String> list = new ArrayList<>();
        list.add("pass");
        map.put("AttributeCodes",list);
        map.put("Type", "last");
        AssetTechnologyAttrResponseDTO roll_bed_rfid_out_welding1 = reqClient.getAssetTechnologyAttrData(accessCode, map);
        List<Results> data= roll_bed_rfid_out_welding1.getResults();
        if (data != null) {
            // 从 data 中获取 Results 对象
            Results results = data.get(0);

            // 从 Results 对象中获取 Series 对象
            List<Series> series = results.getSeries();

            // 从 Series 对象中获取 values 列表
            Series seriesData = series.get(0);

            List<List<Object>> values = seriesData.getValues();
            // 获取 values 列表中第一个元素（即 [1718096668987, String 0, 16]）的第二个值

            int index = values.size();
            log.info("RB764点位值为" + values);
            log.info("RB764点位走向："+values.get(index - 1).get(1)+"，时间："+sdf.format(new Date()));
            if (values.get(index - 1).get(1)!=null) {
                return String.valueOf(values.get(index - 1).get(1));
            } else {
                return "0";
            }
        } else {
            return "0";
        }
    }

    @GetMapping("/readNum96")
    public String readNum96() {
        Map<String, Object> map=new HashMap<>();
        String accessCode="RB753_pass";
        List<String> list = new ArrayList<>();
        list.add("pass");
        map.put("AttributeCodes",list);
        map.put("Type", "last");
        AssetTechnologyAttrResponseDTO roll_bed_rfid_out_welding1 = reqClient.getAssetTechnologyAttrData(accessCode, map);
        List<Results> data = roll_bed_rfid_out_welding1.getResults();
        if (data != null) {
            // 从 data 中获取 Results 对象
            Results results = data.get(0);

            // 从 Results 对象中获取 Series 对象
            List<Series> series = results.getSeries();

            // 从 Series 对象中获取 values 列表
            Series seriesData = series.get(0);

            List<List<Object>> values = seriesData.getValues();
            // 获取 values 列表中第一个元素（即 [1718096668987, String 0, 16]）的第二个值

            int index = values.size();
            log.info("RB753点位走向："+values.get(index - 1).get(1)+"，时间："+sdf.format(new Date()));
            if (values.get(index - 1).get(1)!=null) {
                return String.valueOf(values.get(index - 1).get(1));
            } else {
                return "0";
            }
        } else {
            return "0";
        }
    }

    @GetMapping("/readNum31")
    public String readNum31() {
        Map<String, Object> map=new HashMap<>();
        String accessCode="YHJ31_pass";
        List<String> list = new ArrayList<>();
        list.add("pass");
        map.put("AttributeCodes",list);
        map.put("Type", "last");
        AssetTechnologyAttrResponseDTO roll_bed_rfid_out_welding1 = reqClient.getAssetTechnologyAttrData(accessCode, map);
        List<Results> data = roll_bed_rfid_out_welding1.getResults();
        if (data != null) {
            // 从 data 中获取 Results 对象
            Results results = data.get(0);

            // 从 Results 对象中获取 Series 对象
            List<Series> series = results.getSeries();

            // 从 Series 对象中获取 values 列表
            Series seriesData = series.get(0);

            List<List<Object>> values = seriesData.getValues();
            // 获取 values 列表中第一个元素（即 [1718096668987, String 0, 16]）的第二个值

            int index = values.size();
            log.info("YHJ31点位走向："+values.get(index - 1).get(1)+"，时间："+sdf.format(new Date()));
            if (values.get(index - 1).get(1)!=null) {
                return String.valueOf(values.get(index - 1).get(1));
            } else {
                return "0";
            }
        } else {
            return "0";
        }
    }



    @GetMapping("/readNumOffline")
    public String readNumOffline() {
        Map<String, Object> map=new HashMap<>();
        String accessCode="offline";
        List<String> list = new ArrayList<>();
        list.add("offline");
        map.put("AttributeCodes",list);
        map.put("Type", "last");
        AssetTechnologyAttrResponseDTO roll_bed_rfid_out_welding1 = reqClient.getAssetTechnologyAttrData(accessCode, map);
        List<Results> data= roll_bed_rfid_out_welding1.getResults();
        if (data != null) {
            // 从 data 中获取 Results 对象
            Results results = data.get(0);

            // 从 Results 对象中获取 Series 对象
            List<Series> series = results.getSeries();

            // 从 Series 对象中获取 values 列表
            Series seriesData = series.get(0);

            List<List<Object>> values = seriesData.getValues();
            // 获取 values 列表中第一个元素（即 [1718096668987, String 0, 16]）的第二个值

            int index = values.size();
            log.info("离线点值："+values.get(index - 1).get(1)+"，时间："+sdf.format(new Date()));
            if (values.get(index - 1).get(1)!=null) {
                return String.valueOf(values.get(index - 1).get(1));
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}